from rest_framework import serializers

from goods.models import SKU, GoodsCategory, SKUSpecification


class SKUSpecSerializer(serializers.ModelSerializer):
    spec = serializers.StringRelatedField(read_only=True)
    spec_id = serializers.IntegerField()
    option = serializers.StringRelatedField(read_only=True)
    option_id = serializers.IntegerField()
    sku = serializers.StringRelatedField(read_only=True)
    sku_id = serializers.IntegerField()

    class Meta:
        model = SKUSpecification
        fields = '__all__'


class SKUSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()
    specs = SKUSpecSerializer(read_only=True, many=True)

    class Meta:
        model = SKU
        fields = '__all__'


class GoodsCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ['id', 'name']
